package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.generator.IFill;
import org.example.config.MyBloomFilter;
import org.example.entity.Air;
import org.example.service.IAirService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-05-08
 */
@RestController
@RequestMapping("/air")
public class AirController {

    @Autowired
    private IAirService airService;
    @Autowired
    private MyBloomFilter myBloomFilter;

    @GetMapping
    public Page<Air> getAll(Air air , int page, int size){
//        QueryWrapper wrapper = Wrappers.query()
//                .like(StringUtils.hasText(air.getMonitoringStation()),"monitoringStation", air.getMonitoringStation())
//                .eq(air.getDistrictid() != null, "districtid", air.getDistrictid());

        LambdaQueryWrapper<Air> queryWrapper = Wrappers.lambdaQuery(Air.class)
                .like(StringUtils.hasText(air.getMonitoringStation()), Air::getMonitoringStation, air.getMonitoringStation())
                .eq(air.getDistrictid() != null, Air::getDistrictid , air.getDistrictid());
        return airService.page(Page.of(page,4), queryWrapper);

    }

    @GetMapping("/{id}")
    public Air get(@PathVariable Long id){
        if (myBloomFilter.bloomFilter.mightContain(id)){
            System.out.println("id" + id + "存在");
            return airService.getById(id);
        }else {
            System.out.println("id" + id + "不存在");
            return null;
        }
    }

    @PostMapping
    public Air add(@RequestBody Air air){
        return airService.saveWithCache(air);
    }

    @DeleteMapping("/{id}")
    public boolean del (@PathVariable Long id){
        return airService.removeById(id);
    }

    @PutMapping
    public Air update(@RequestBody Air air){
        return airService.updateByIdWithCache(air);
    }


}
